<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/11/6 15:05
 * Desc: 积分应用
 */
class Model_Api_V2_Marketing_Strategy_Integral extends Contract_Api_Marketing_Strategy
{
    private static $instance = null;

    private $type_id;

    private $application_name = '积分';

    private $application_code = 'system_integral';

    public function __construct($type_id)
    {
        $this->type_id = $type_id;
    }


    /**
     * @param int $type_id
     * @param array $params
     * @return Model_Api_V2_Marketing_Strategy_Integral|null
     */
    public static function get_instance(int $type_id, array $params = array())
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($type_id);
        }

        return self::$instance;
    }

    /**
     * 检查是否支持对应产品
     * 支持返回code和中文名称
     * 不支持返回 false
     * @param $product_id
     * @return array|false
     */
    public function is_support($product_id)
    {
        $product = St_Product::get_product_info($this->type_id, $product_id);
        if ($product) {
            $data = array();
            // 预定送积分
            $book_id     = $product['jifenbook_id'] ?? 0;
            $book_config = Model_Jifen::get_used_jifenbook($book_id, $this->type_id);
            if ($book_config) {
                $data[] = array(
                    'code' => 'send_integral',
                    'name' => '送积分',
                    'icon' => '&#xe640;'
                );
            }

            // 积分抵现配置
            $price_id     = $product['jifentprice_id'] ?? 0;
            $price_config = Model_Jifen_Price::get_used_jifentprice($price_id, $this->type_id);
            if ($price_config) {
                $data[] = array(
                    'code' => 'use_integral',
                    'name' => '积分抵扣',
                    'icon' => '&#xe642;'
                );
            }

            if (!empty($data)) {
                return $data;
            }
        }

        return false;
    }
}